package rs.etf.es.visualization;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JFileChooser;

public class LoadGraphEventHandler implements ActionListener {

    private MainWindow view;

    public LoadGraphEventHandler(MainWindow mainWindow) {
	view = mainWindow;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
	int returnVal = view.getFileChooser().showOpenDialog(view.frmBayesianNetworkSimulator);
	if (returnVal == JFileChooser.APPROVE_OPTION) {
	    File selectedFile = view.getFileChooser().getSelectedFile();
	    AlgorithmController.instance().setData(view);

	    try {
		view.enableAlgorithmControllButtons(true);
		AlgorithmController.instance().loadGraphFromFile(selectedFile);
		AlgorithmController.instance().createGraph();
		AlgorithmController.instance().startAlgorithm();
	    } catch (FileNotFoundException e1) {
		e1.printStackTrace();
	    } catch (IOException e1) {
		e1.printStackTrace();
	    }
	}

    }
}
